package com.isunland.managesystem.base;

import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import butterknife.ButterKnife;
import java.util.ArrayList;
import java.util.List;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 3/30/2017 9:16 AM
 */

public abstract class BaseButterKnifeAdapter<T> extends BaseAdapter {

  protected List<T> mList = new ArrayList<>();
  protected BaseVolleyActivity context;
  protected LayoutInflater layoutInflater;

  public BaseButterKnifeAdapter(BaseVolleyActivity context, ArrayList<T> objects) {
    this.context = context;
    this.layoutInflater = LayoutInflater.from(context);
    this.mList = objects;
  }

  @Override public int getCount() {
    return mList.size();
  }

  @Override public T getItem(int position) {
    return mList.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @SuppressWarnings("all") @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = layoutInflater.inflate(setLayoutId(), parent, false);
      convertView.setTag(initHolder(convertView));
    }

    initView(getItem(position), (BaseViewHolder) convertView.getTag(), position);
    return convertView;
  }

  protected abstract int setLayoutId();

  @NonNull protected abstract BaseViewHolder initHolder(View convertView);

  protected abstract void initView(T item, BaseViewHolder baseHolder, int position);

  /**
   * 需要声明butterKnife的引用.
   */
  protected abstract class BaseViewHolder {
    protected BaseViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
